package com.leo.customer.service.impl

import com.leo.customer.entity.dao.WorkLabelDao
import com.leo.customer.entity.pojo.WorkLabel
import com.leo.customer.entity.pojo.WorkLabelVo
import com.leo.customer.service.WorkLabelService
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class WorkLabelServiceImpl : WorkLabelService {

    @Autowired
    lateinit var workLabelDao: WorkLabelDao
    override fun addWorkLabel(workLabel: WorkLabel): MutableList<WorkLabelVo> {
        if (workLabelDao.findWorkLabelByName(workLabel.name) == null)
            workLabelDao.save(workLabel)
        return getAllLabel()
    }

    override fun getAllLabel(): MutableList<WorkLabelVo> {
        val labelsVo = mutableListOf<WorkLabelVo>()
        workLabelDao.findAll().forEach {
            val workLabelVo = WorkLabelVo()
            BeanUtils.copyProperties(it, workLabelVo)
            labelsVo.add(workLabelVo)
        }
        return labelsVo
    }
}
